package com.google.samples.apps.library_common.extension.extension

import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.samples.apps.library_common.extension.load
import com.google.samples.apps.library_common.extension.loadAvatar
import com.google.samples.apps.library_common.extension.loadDefault
import com.google.samples.apps.library_common.extension.loadFillet


fun BaseViewHolder.loadAvatar(@IdRes viewId: Int, @DrawableRes drawableRes: Int): BaseViewHolder {
    getView<ImageView>(viewId).loadAvatar(drawableRes)
    return this
}

fun BaseViewHolder.loadAvatar(@IdRes viewId: Int, url: String?): BaseViewHolder {
    getView<ImageView>(viewId).loadAvatar(url)
    return this
}


fun BaseViewHolder.loadImage(@IdRes viewId: Int, @DrawableRes drawableRes: Int): BaseViewHolder {
    getView<ImageView>(viewId).load(drawableRes)
    return this
}

fun BaseViewHolder.loadImage(@IdRes viewId: Int, url: String?): BaseViewHolder {
    getView<ImageView>(viewId).load(url)
    return this
}

fun BaseViewHolder.loadLv(@IdRes viewId: Int, url: String?): BaseViewHolder {
    getView<ImageView>(viewId).loadDefault(url)
    return this
}

fun BaseViewHolder.loadImageFillet(@IdRes viewId: Int, url: String?, radius: Int): BaseViewHolder {
    getView<ImageView>(viewId).loadFillet(url, radius)
    return this
}

/**
 * auto： true:判断value是为空，空GONE；
 */
fun BaseViewHolder.setText(@IdRes viewId: Int, value: CharSequence?, autoGone: Boolean): BaseViewHolder {
    getView<TextView>(viewId).text = value
    if (autoGone)
        getView<TextView>(viewId).visibility = if (TextUtils.isEmpty(value)) View.GONE else View.VISIBLE
    return this
}

fun BaseViewHolder.setHint(viewId: Int, strId: Int): BaseViewHolder {
    getView<EditText>(viewId).setHint(strId)
    return this
}

fun BaseViewHolder.setHint(viewId: Int, value: CharSequence?): BaseViewHolder {
    getView<EditText>(viewId).setHint(value)
    return this
}

fun BaseViewHolder.setInputType(viewId: Int, value: Int): BaseViewHolder {
    getView<EditText>(viewId).inputType = value
//            InputType.TYPE_NULL
    return this
}

fun BaseViewHolder.setOnClickListener(listener: View.OnClickListener): BaseViewHolder {
    itemView.setOnClickListener(listener)
    return this
}

fun BaseViewHolder.setOnClickListener(@IdRes viewId: Int, listener: View.OnClickListener): BaseViewHolder {
    getView<View>(viewId).setOnClickListener(listener)
    return this
}

fun BaseViewHolder.setOnClickListener(viewIds: List<Int>, listener: View.OnClickListener): BaseViewHolder {
    viewIds.forEach { viewId ->
        setOnClickListener(viewId, listener)
    }
    return this
}

fun BaseViewHolder.setOnLongClickListener(listener: View.OnLongClickListener): BaseViewHolder {
    itemView.setOnLongClickListener(listener)
    return this
}

fun BaseViewHolder.setOnLongClickListener(@IdRes viewId: Int, listener: View.OnLongClickListener): BaseViewHolder {
    getView<View>(viewId).setOnLongClickListener(listener)
    return this
}


fun BaseViewHolder.setVisible(viewId: Int, isVisible: Boolean): BaseViewHolder {
    val view = getView<View>(viewId)
    view.visibility = if (isVisible) View.VISIBLE else View.GONE
    return this
}

fun BaseViewHolder.setInvisible(viewId: Int, isInvisible: Boolean): BaseViewHolder {
    val view = getView<View>(viewId)
    view.visibility = if (isInvisible) View.INVISIBLE else View.VISIBLE
    return this
}
